package com.xp.tinyurl.admin.controller;

import com.xp.tinyurl.admin.dto.TinyUrlGenDTO;
import com.xp.tinyurl.admin.dto.TinyUrlQueryDTO;
import com.xp.tinyurl.admin.vo.ShortVO;
import com.xp.tinyurl.common.base.PageList;
import com.xp.tinyurl.common.base.ResultVO;
import com.xp.tinyurl.common.base.ResultVOBuilder;
import com.xp.tinyurl.common.dto.UShorterUrlDTO;
import com.xp.tinyurl.common.enums.BusinessLineEnum;
import com.xp.tinyurl.common.utils.BinaryUtil;
import com.xp.tinyurl.common.utils.DateUtils;
import com.xp.tinyurl.model.ShortUrlMap;
import com.xp.tinyurl.service.ShortUrlMapSddlService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description 短连接管理
 * @Author chengguo.pei
 * @Date 2021/9/22 18:00
 */
@Slf4j
@RequestMapping("/admin/tinyUrl")
@RestController
public class TinyUrlController {

    @Autowired
    private ShortUrlMapSddlService shortUrlMapSddlService;

    /**
     * 生成短链接
     * @param tinyUrlGenDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "create")
    public ResultVO create(TinyUrlGenDTO tinyUrlGenDTO) {

        UShorterUrlDTO shorterUrlDTO = buildToUShorterUrlDTO(tinyUrlGenDTO);
        //create
        ShortUrlMap shortUrlMap = shortUrlMapSddlService.createTinyUrl(shorterUrlDTO);
        //build response
        ShortVO shortVO = new ShortVO();
        shortVO.setShortUrl(shortUrlMap.getLongUrl());
        shortVO.setExpired(DateUtils.longToStr(shortUrlMap.getExpireTime().getTime()));
        return ResultVOBuilder.success(shortVO);
    }

    /**
     * build 入参
     * @param tinyUrlGenDTO
     * @return
     */
    private UShorterUrlDTO buildToUShorterUrlDTO(TinyUrlGenDTO tinyUrlGenDTO){
        UShorterUrlDTO shorterUrlDTO = new UShorterUrlDTO();
        shorterUrlDTO.setBusinessLine(tinyUrlGenDTO.getBusinessLine());
        shorterUrlDTO.setChannelId(tinyUrlGenDTO.getChannelId());
        shorterUrlDTO.setLongUrl(tinyUrlGenDTO.getLongUrl());
        shorterUrlDTO.setExpired(tinyUrlGenDTO.getExpired());
        return shorterUrlDTO;
    }

    /**
     * 短链接列表
     * @param queryDTO
     * @return
     */
    @RequestMapping(value = "/pageList")
    public ResultVO pageList(TinyUrlQueryDTO queryDTO) {
        if(queryDTO.getPageNo() == null){
            queryDTO.setPageNo(1);
        }
        if(queryDTO.getPageSize() == null){
            queryDTO.setPageSize(15);
        }
        if(!ObjectUtils.isEmpty(queryDTO.getShortUrl())){
            String suffixUrl = queryDTO.getShortUrl().substring(queryDTO.getShortUrl().lastIndexOf("/")+1);
            queryDTO.setUuid(BinaryUtil.toBase10(suffixUrl));
        }
        PageInfo<ShortUrlMap> pageInfo = shortUrlMapSddlService.pageList(queryDTO);
        PageList<ShortUrlMap> pageList = PageList.build(pageInfo.getList(), pageInfo.getTotal());
        if(!CollectionUtils.isEmpty(pageList.getItems())){
            pageList.getItems().forEach(shortUrlMap -> {
                shortUrlMap.setBusinessLineName(BusinessLineEnum.getByCode(shortUrlMap.getBusinessLine()).getDesc());
            });
        }
        return ResultVOBuilder.success(pageList);

    }

}
